﻿using System;
using System.Collections.Generic;
using System.Text;
using Google.GData.Client;
using Google.Documents;
using Google.GData.Apps;
using System.Collections;

namespace GDocBackupLib
{

    // this is only a "copy&paste" for testing compilation

    public class Fhtino_Code
    {
        public void TestGetDocumentsForDomain(string domain, string oauthKey, string login, string password)
        {
            string[] usernames = this.GetUsersForDomain(domain, login, password);
            foreach (string username in usernames)
            {
                List<Document> docList = GetDocumentsForUser_ByFHTINO(domain, oauthKey, username);
            }
        }

        public string[] GetUsersForDomain(string domain, string username, string password)
        {
            // This feed requires the username to be fully qualified with the domain
            username = AppendDomainIfNeeded(username, domain);
            AppsService appsServ = new AppsService(domain, username, password);
            UserQuery uQuery = new UserQuery(domain, true);
            //uQuery.Uri = new OAuthUri(
            //    "http://apps-apis.google.com/a/feeds/predictiveservice.com/user/2.0",
            //    username, domain);
            // You don't need this. The URI is just set by  UserQuery uQuery = new UserQuery(domain, true);

            ArrayList usernames = new ArrayList();
            try
            {
                UserFeed usersFeed = appsServ.RetrieveAllUsers();
                foreach (UserEntry entry in usersFeed.Entries)
                {
                    usernames.Add(entry.Login.UserName);
                }
            }
            catch (Exception e)
            {
                throw new Exception("Unable to retrieve a list of users.", e);
            }
            return (string[])usernames.ToArray(typeof(String));
        }

        public List<Document> GetDocumentsForUser_ByFHTINO(string domain, string OAuthSecret, string username)
        {
            // NOT REQUIRED
            //GOAuthRequestFactory requestFactory = new GOAuthRequestFactory("GDocGetDocuments", APPLICATION_NAME);
            //requestFactory.ConsumerKey = domain;
            //requestFactory.ConsumerSecret = OAuthSecret;

            String consumerKey = domain;   // ???  RIGHT?
            RequestSettings settings = new RequestSettings("GDocBackup-NEW2", consumerKey, OAuthSecret, username, domain);

            DocumentsRequest request = new DocumentsRequest(settings);

            // Get doc list from GDocs
            Feed<Document> feed = request.GetEverything();
            List<Document> docs = new List<Document>();
            foreach (Document entry in feed.Entries)
                docs.Add(entry);

            return docs;
        }


        private string AppendDomainIfNeeded(string username, string domain)
        {
            if (!username.ToLower().EndsWith(domain.ToLower()))
            {
                username = username + "@" + domain;
            }
            return username;
        }
    }
}
